<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Top extends BaseController {
	
	function Top(){
		parent::BaseController();
		
		$this->load->model("Node_Model", "Nodes");
	}
	
	function index(){
		$this->all();
	}
	
	function day(){
		$node_params = array(
			"order" => array("hit.day" => "desc")
		);
		
		$this->_get($node_params);
	}
	
	function week(){
		$node_params = array(
			"order" => array("hit.week" => "desc")
		);
		
		$this->_get($node_params);
	}
	
	function month(){
		
		$node_params = array(
			"order" => array("hit.month" => "desc")
		);
		
		$this->_get($node_params);
	}
	
	function year(){
		$node_params = array(
			"order" => array("hit.year" => "desc")
		);
		
		$this->_get($node_params);
	}
	
	function all(){
		
		$node_params = array(
			"order" => array("hit.total" => "desc")
		);
		
		$this->_get($node_params);
	}
	
	function _get($node_params){
		$cache_time = intval($this->settings->item("_site_cache_nodes_time"));
		
		$nodes = $this->mp_cache->model("Nodes", "get", array($node_params), $cache_time);
		
		$nodes_process = array();
		
		foreach($nodes as $node){
			$node->categories = $this->mp_cache->model("Nodes", "getCategories", array($node->id), $cache_time);
			$node->metas = $this->mp_cache->model("Nodes", "getMeta", array($node->id), $cache_time);
			$node->hits = $this->mp_cache->model("Nodes", "getHit", array($node->id), $cache_time);
			
			array_push($nodes_process, $node);
		}
		
		$nodes = $nodes_process;
		
		$this->template->addData("nodes", $nodes);
		
		$this->template->render();
	}
	
}